package com.oa.user.BizImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.oa.entity.TblUser;
import com.oa.user.Biz.UserBiz;
import com.oa.user.Dao.UserDao;

public class UserBizImpl implements UserBiz {
	
	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	/*
	 * 信息加载
	 * @see com.oa.user.Biz.UserBiz#load(java.lang.Integer)
	 */
	@Override
	public TblUser load(Integer Id) {
		TblUser tu = this.userDao.load(Id);
		return tu;
	}
	/*
	 * 信息添加
	 * @see com.oa.user.Biz.UserBiz#add(com.oa.entity.TblUser)
	 */
	@Override
	public boolean add(TblUser user) {
		//需要验证用户的合法性：重名、
		boolean flag = this.userDao.add(user);
		return flag;
	}
	/*
	 * 信息删除
	 * @see com.oa.user.Biz.UserBiz#delete(com.oa.entity.TblUser)
	 */
	@Override
	public boolean delete(TblUser user) {
		boolean flag = false;
		try {
			TblUser tu = this.load(user.getUserId());
			return this.userDao.delete(tu);
		} catch (Exception e) {
			return flag;
		}
	}
	/*
	 * 信息更新
	 * @see com.oa.user.Biz.UserBiz#update(com.oa.entity.TblUser)
	 */
	@Override
	public boolean update(TblUser user) {
		boolean flag = this.userDao.update(user);
		return flag;
	}
	/*
	 * 验证数据的合法性：是否重名
	 * @see com.oa.user.Biz.UserBiz#checkLegitimacy(java.lang.String)
	 */
	@Override
	public boolean checkLegitimacy(String username) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Map params = new HashMap();
		params.put("username",username);
		String hql = "select u from TblUser u where 1=1 and u.username=:username";
		List<TblUser> ulist = searchData(hql,params);
		if(ulist!=null&&ulist.size()>0){
			flag = true;
		}
		return flag;
	}
	/*
	 * 验证用户登录：用户名、密码
	 * @see com.oa.user.Biz.UserBiz#login(com.oa.entity.TblUser)
	 */
	@Override
	public boolean login(TblUser tu) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Map params  = new HashMap();
		String hql = "select u from TblUser u where 1=1";
		if(tu.getUsername()!=null&&tu.getPassword()!=null){
			params.put("username", tu.getUsername());
			hql += " and u.username=:username";
		}
		List<TblUser> ulist = this.userDao.searchTargetDatas(hql, params);
		if(ulist!=null&&ulist.size()>0){
			flag = true;
		}
		return flag;
	}

	@Override
	public List<TblUser> searchData(String hql, Map params) {
		// TODO Auto-generated method stub
		List<TblUser> ulist = this.userDao.searchTargetDatas(hql, params);
		return ulist;
	}
}
